import { baseURL } from "./request";
import Taro from "@tarojs/taro";

export const objToParams = (obj) => {
    let str = "?";
    for (const objKey in obj) {
        str+=`${objKey}=${obj[objKey]}&`
    }
    return str.slice(0,-1);
}

export const selectImage = (sourceType=['album', 'camera'],count=1) => {
    return new Promise((resolve, reject) => {
        Taro.chooseImage({
            sizeType: ['original', 'compressed'],
            sourceType,
            count
        }).then(res => {
            resolve(res.tempFilePaths);
            console.log(res.tempFilePaths,"选择文件结果")
        }).catch(async err => {
            await Taro.showToast({
                title:"取消选择",
                icon:"none"
            })
            reject()
        })
    })
}

export const uploadFile = (filePath,name="files") => {
  return new Promise((resolve, reject) => {
    Taro.uploadFile({
        url:baseURL+"/app-api/databoard/mini/upload-file",
        name,filePath,
    }).then(res => {
        console.log(res,"上传文件结果")
        resolve(res);
    }).catch(err => {
        reject(err);
    })
  })
}

export const deepClone = (obj) => {
    const objClone = Array.isArray(obj)?[]:{};
    if(obj && typeof obj==="object"){
        for(const key in obj){
            if(obj.hasOwnProperty(key)){
                //判断ojb子元素是否为对象，如果是，递归复制
                if(obj[key]&&typeof obj[key] ==="object"){
                    objClone[key] = deepClone(obj[key]);
                }else{
                    //如果不是，简单复制
                    objClone[key] = obj[key];
                }
            }
        }
    }
    return objClone;
}